package com.honey.mybatis.generator.inversion;

import com.honey.compilation.java.Field;
import com.honey.compilation.java.JavaKeyWord;
import com.honey.compilation.java.Method;
import com.honey.compilation.java.Parameter;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.core.utils.JavaBeansUtil;
import com.honey.mybatis.generator.dependent.DependentModel;

class InjectionModel implements Inversion{
	
	private static final  String VARIABLE="myBatisSqlSession";
	
	private String getVariableName(String variable){
		String answer = VARIABLE ; 
		if(variable != null && variable.length() != 0  ){
			answer = variable;
		}
		return answer ;
	}
	
	@Override
	public FullyQualifiedJavaType extend() {
		return null;
	}

	@Override
	public Field[] inversionFiled(String fieldname ) {
		return new Field[]{ 
			new Field(getVariableName(fieldname) , SQL_SESSION,"Mybatis的操作数据库使用的dao对象"),
		};
	}
	
	@Override
	public Method[] inversionMethod(String fieldname) {
		String var = getVariableName(fieldname);
		Method[] answer = new Method[2];
		answer[0] = new Method( 
				JavaBeansUtil.getGetterMethodName(var, SQL_SESSION),
				SQL_SESSION 
		) ;
		
		answer[0].addBodyLine("return this." + var);
		answer[0].addJavaDocLine("获取SqlSession");
		answer[1] = new Method(JavaBeansUtil.getSetterMethodName(var)) ;
		answer[1] .addParameter(new Parameter(SQL_SESSION, var) );
		answer[1].addBodyLine("this." + var +" = "+var);
		answer[1].addJavaDocLine("使用注入的方式创建mybatis的SqlSessionDaoSupport。");
		return answer;
	}

	@Override
	public String call(DependentModel dependentModel, String variable) {
		return ( 
					dependentModel == null ? 
					JavaKeyWord.OPERATION_THIS
						:
					dependentModel.call(InversionModel.INJECTION) 
				)+ 
				JavaBeansUtil.getGetterMethodName(getVariableName(variable), SQL_SESSION)+
				"( )";
	}
}
